package com.liang.lingdong.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liang.lingdong.common.ErrorCode;
import com.liang.lingdong.exception.BusinessException;
import com.liang.lingdong.model.exam.Exam;
import com.liang.lingdong.model.sign.Sign;
import com.liang.lingdong.model.sign.dto.SignUpDto;
import com.liang.lingdong.service.ExamService;
import com.liang.lingdong.service.SignService;
import com.liang.lingdong.mapper.SignMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
* @author liang
* @description 针对表【sign(用户报名表)】的数据库操作Service实现
* @createDate 2024-11-29 21:34:17
*/
@Service
public class SignServiceImpl extends ServiceImpl<SignMapper, Sign>
    implements SignService{


    @Resource
    private ExamService examService;

    /**
     * 报名
     * @param signUpDto 报名信息
     * @return 结果
     */
    @Override
    public boolean signUp(SignUpDto signUpDto) {
        Long examId = signUpDto.getExamId();
        // 校验考试信息
        Exam exam = examService.lambdaQuery()
                .eq(Exam::getBusinessId, examId)
                .one();
        if (exam == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"考试不存在");
        }
        // 判断考试是否进行中或者已结束
        if (new Date().after(exam.getStartTime()) && new Date().before(exam.getEndTime())) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"考试进行中或已结束，无法报名");
        }
        // 校验是否重复报名
        Long userId = StpUtil.getLoginIdAsLong();
        Sign sign = lambdaQuery()
                .eq(Sign::getExamId, examId)
                .eq(Sign::getUserId, userId)
                .one();
        if (sign != null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"已报名该考试");
        }
        // 创建报名
        Sign createSign = new Sign();
        createSign.setExamId(examId);
        createSign.setUserId(userId);
        createSign.setStatus(0);
        return save(createSign);
    }

    /**
     * 取消报名
     * @param signUpDto 报名信息
     * @return 结果
     */
    @Override
    public boolean cancelSignUp(SignUpDto signUpDto) {
        Long examId = signUpDto.getExamId();
        // 校验考试信息
        Exam exam = examService.lambdaQuery()
                .eq(Exam::getBusinessId, examId)
                .one();
        if (exam == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"考试不存在");
        }
        // 判断考试是否进行中或者已结束
        if (new Date().after(exam.getStartTime()) && new Date().before(exam.getEndTime())) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"考试进行中或已结束，无法报名");
        }
        // 校验是否报名
        Long userId = StpUtil.getLoginIdAsLong();
        Sign sign = lambdaQuery()
                .eq(Sign::getExamId, examId)
                .eq(Sign::getUserId, userId)
                .one();
        if (sign == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"未报名该考试");
        }
        return removeById(sign);
    }

    /**
     * 获取报名信息
     * @param examId 考试id
     * @return 报名信息
     */
    @Override
    public Long getSignInfo(Long examId) {
        Long userId = StpUtil.getLoginIdAsLong();
        // 获取比赛
        Exam exam = examService.lambdaQuery()
                .eq(Exam::getBusinessId, examId)
                .one();
        if (exam == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"考试不存在");
        }
        Sign sign = lambdaQuery()
                .eq(Sign::getExamId, examId)
                .eq(Sign::getUserId, userId)
                .one();
        if (sign == null) {
           throw new BusinessException(ErrorCode.PARAMS_ERROR,"请先报名该考试");
        }
        return sign.getBusinessId();
    }
}




